package com.qingfeng.base.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qingfeng.base.aop.LogModel;
import com.qingfeng.base.dao.TRoleMapper;
import com.qingfeng.base.model.TRole;
import com.qingfeng.base.mpUtil.FBaseMapper;
import com.qingfeng.base.service.TRoleService;
import com.qingfeng.base.util.CodeEnum;
import com.qingfeng.base.util.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2019/9/18 4:46 下午
 */
@LogModel("角色管理")
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController<TRole>{

    @Autowired
    private TRoleMapper tRoleMapper;

    @Autowired
    private TRoleService tRoleService;

    @Override
    public FBaseMapper<TRole> getMapper() {
        return tRoleMapper;
    }

    /**
     * 设置权限
     *
     * @param json
     * @return
     */
    @ApiOperation("设置权限")
    @PostMapping("/insert-permit")
    public ResponseMessage setRolePermission(@RequestBody JSONObject json){
        List<String> permissions = JSON.parseArray(json.getJSONArray("list").toJSONString(),String.class);
        String roleId = json.getString("object");
        if (StringUtils.isEmpty(roleId)){
            return failure();
        }
        int index = tRoleService.addPermission(roleId,permissions);
        return success(index);
    }

    @ApiOperation("删除")
    @ApiImplicitParam(name = "ids", value = "主键ids", required = true)
    @PostMapping("/delete")
    @ResponseBody
    @Override
    public ResponseMessage delete(@RequestBody JSONArray ids){
        List<String> idList =  JSON.parseArray(ids.toJSONString(),String.class);
        tRoleService.deleteList(idList);
        return new ResponseMessage(CodeEnum.SUCCESS,"");

    }

}
